import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.util.Vector;

public class ScheduleSettings {

	private static String className = "ScheduleSettings: ";
	private static String settingsFilePath = "config//schedule_settings.xml";

	public static int scheduleTickInterval = 1000;
	public static String tdToolFilePath = "/usr/bin/tdtool";

	public static void saveSettings(Vector<ScheduleEvent> scheduleEvents) {
		DEBUG("saveSettings");

		Properties properties = new Properties();

		// START CONFIG
		for (int i = 0; i < scheduleEvents.size(); i++) {
			ScheduleEvent scheduleEvent = scheduleEvents.get(i);
			properties.setProperty(i + "_switchOn",
					Boolean.toString(scheduleEvent.switchOn));
			properties.setProperty(i + "_weekdays",
					Short.toString(scheduleEvent.weekdays));
			properties.setProperty(i + "_treatHolidayAsSunday",
					Boolean.toString(scheduleEvent.treatHolidayAsSunday));
			properties.setProperty(i + "_eventType",
					scheduleEvent.eventType.toString());
			properties.setProperty(i + "_startHour",
					Integer.toString((scheduleEvent.startHour)));
			properties.setProperty(i + "_startMinute",
					Integer.toString((scheduleEvent.startMinute)));
			properties.setProperty(i + "_sunOffSetMinutes",
					Integer.toString((scheduleEvent.sunOffSetMinutes)));
			properties.setProperty(i + "_devices",
					Long.toString((scheduleEvent.devices)));

		}
		// END CONFIG

		try {
			FileOutputStream fos = new FileOutputStream(settingsFilePath);
			properties.storeToXML(fos, "ScheduleSettings", "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void loadSettings(Vector<ScheduleEvent> scheduleEvents) {
		DEBUG("loadSettings " + settingsFilePath);
		Properties properties = new Properties();

		try {
			FileInputStream fis = new FileInputStream(settingsFilePath);
			properties.loadFromXML(fis);
		} catch (InvalidPropertiesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}

		// START CONFIG
		String valueString;
		int i = 0;

		while (true) {
			ScheduleEvent scheduleEvent = new ScheduleEvent();

			valueString = properties.getProperty(i + "_switchOn");
			if (valueString != null) {
				scheduleEvent.switchOn = Boolean.parseBoolean(valueString);
			} else {
				break;
			}
			valueString = properties.getProperty(i + "_weekdays");
			if (valueString != null) {
				scheduleEvent.weekdays = Byte.parseByte((valueString));
			} else {
				break;
			}
			valueString = properties.getProperty(i + "_treatHolidayAsSunday");
			if (valueString != null) {
				scheduleEvent.treatHolidayAsSunday = Boolean
						.parseBoolean(valueString);
			} else {
				break;
			}
			valueString = properties.getProperty(i + "_eventType");
			if (valueString != null) {
				scheduleEvent.eventType = EventType.valueOf(valueString);
			} else {
				break;
			}
			valueString = properties.getProperty(i + "_startHour");
			if (valueString != null) {
				scheduleEvent.startHour = Byte.parseByte((valueString));
			} else {
				break;
			}
			valueString = properties.getProperty(i + "_startMinute");
			if (valueString != null) {
				scheduleEvent.startMinute = Byte.parseByte((valueString));
			} else {
				break;
			}
			valueString = properties.getProperty(i + "_sunOffSetMinutes");
			if (valueString != null) {
				scheduleEvent.sunOffSetMinutes = Byte.parseByte((valueString));
			} else {
				break;
			}
			valueString = properties.getProperty(i + "_devices");
			if (valueString != null) {
				scheduleEvent.devices = Long.parseLong((valueString));
			} else {
				break;
			}

			scheduleEvents.add(scheduleEvent);
			i++;
		}
		// END CONFIG
	}

	private static void DEBUG(String debugString) {
		Macros.DEBUG(className + debugString);
	}
}
